﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class IdleInventoryRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15266;
        protected GridView gvIdleInventoryRpt;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblIdleInventoryRptList;
        protected Label lblMsg;
        protected Label lblTotalLocCount;
        protected Label lblTotalLocCountCaption;
        protected Label lblTotalQty;
        protected Label lblTotalQtyCaption;
        private List<string> locList = new List<string>();
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalQty = 0M;
        protected UpdatePanel up1;

        private void BindData2IdleInventoryRptList(IdleInventoryRptQueryEntiry idleInventoryRptQueryEntiry)
        {
            this.PagerFooter.CurrentPage = int.Parse(idleInventoryRptQueryEntiry.CurrentPage);
            DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetIdleInventoryRptDataSet(idleInventoryRptQueryEntiry);
            base.BindData2GridView(this.gvIdleInventoryRpt, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            IdleInventoryRptQueryEntiry idleInventoryRptQueryEntiry = this.GetIdleInventoryRptQueryEntiry();
            idleInventoryRptQueryEntiry.IsGetAll = true;
            string currentPageIndex = idleInventoryRptQueryEntiry.CurrentPage;
            idleInventoryRptQueryEntiry.CurrentPage = "0";
            this.gvIdleInventoryRpt.Columns[1].Visible = false;
            this.gvIdleInventoryRpt.AllowSorting = false;
            this.BindData2IdleInventoryRptList(idleInventoryRptQueryEntiry);
            this.gvIdleInventoryRpt.ShowFooter = false;
            idleInventoryRptQueryEntiry.IsGetAll = false;
            idleInventoryRptQueryEntiry.CurrentPage = currentPageIndex;
            this.SetIdleInventoryRptQueryEntiry(idleInventoryRptQueryEntiry);
            base.Export2Excel("IdleInventoryRpt.xls", this.gvIdleInventoryRpt);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            IdleInventoryRptQueryEntiry idleInventoryRptQueryEntiry = this.GetIdleInventoryRptQueryEntiry();
            idleInventoryRptQueryEntiry.CurrentPage = "0";
            this.BindData2IdleInventoryRptList(idleInventoryRptQueryEntiry);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            IdleInventoryRptQueryEntiry idleInventoryRptQueryEntiry = this.GetIdleInventoryRptQueryEntiry();
            idleInventoryRptQueryEntiry.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2IdleInventoryRptList(idleInventoryRptQueryEntiry);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            IdleInventoryRptQueryEntiry idleInventoryRptQueryEntiry = this.GetIdleInventoryRptQueryEntiry();
            idleInventoryRptQueryEntiry.OWNER_ID = "";
            idleInventoryRptQueryEntiry.VENDOR_ID = "";
            idleInventoryRptQueryEntiry.MIN_DAYS = 0;
            idleInventoryRptQueryEntiry.MAX_DAYS = 0;
            this.BindData2IdleInventoryRptList(idleInventoryRptQueryEntiry);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            IdleInventoryRptQueryEntiry idleInventoryRptQueryEntiry = this.GetIdleInventoryRptQueryEntiry();
            if (int.Parse(idleInventoryRptQueryEntiry.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                idleInventoryRptQueryEntiry.CurrentPage = (int.Parse(idleInventoryRptQueryEntiry.CurrentPage) + 1).ToString();
                this.BindData2IdleInventoryRptList(idleInventoryRptQueryEntiry);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            IdleInventoryRptQueryEntiry idleInventoryRptQueryEntiry = this.GetIdleInventoryRptQueryEntiry();
            if (int.Parse(idleInventoryRptQueryEntiry.CurrentPage) > 0)
            {
                idleInventoryRptQueryEntiry.CurrentPage = (int.Parse(idleInventoryRptQueryEntiry.CurrentPage) - 1).ToString();
                this.BindData2IdleInventoryRptList(idleInventoryRptQueryEntiry);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            IdleInventoryRptQueryEntiry idleInventoryRptQueryEntiry = this.GetIdleInventoryRptQueryEntiry();
            this.BindData2IdleInventoryRptList(idleInventoryRptQueryEntiry);
        }

        private IdleInventoryRptQueryEntiry GetIdleInventoryRptQueryEntiry()
        {
            return (base.PageQueryEntitySession as IdleInventoryRptQueryEntiry);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                IdleInventoryRptQueryEntiry idleInventoryRptQueryEntiry = this.GetIdleInventoryRptQueryEntiry();
                idleInventoryRptQueryEntiry.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2IdleInventoryRptList(idleInventoryRptQueryEntiry);
            }
        }

        protected void gvIdleInventoryRpt_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15266", this.gvIdleInventoryRpt);
        }

        protected void gvIdleInventoryRpt_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (((Label) e.Row.FindControl("lblLOC")) != null)
                {
                    string loc = ((Label) e.Row.FindControl("lblLOC")).Text;
                    if (!this.locList.Contains(loc))
                    {
                        this.locList.Add(loc);
                    }
                }
                if ((((Label) e.Row.FindControl("lblQTY")) != null) && !string.IsNullOrEmpty(((Label) e.Row.FindControl("lblQTY")).Text))
                {
                    this.TotalQty += decimal.Parse(((Label) e.Row.FindControl("lblQTY")).Text);
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalLocCount.Text = this.locList.Count.ToString();
                this.lblTotalQty.Text = base.GetPreferenceDigit(this.TotalQty);
            }
        }

        protected void gvIdleInventoryRpt_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                IdleInventoryRptQueryEntiry idleInventoryRptQueryEntiry = this.GetIdleInventoryRptQueryEntiry();
                idleInventoryRptQueryEntiry.SortField = e.SortExpression;
                if (idleInventoryRptQueryEntiry.SortDirection == "ASC")
                {
                    idleInventoryRptQueryEntiry.SortDirection = "DESC";
                }
                else if (idleInventoryRptQueryEntiry.SortDirection == "DESC")
                {
                    idleInventoryRptQueryEntiry.SortDirection = "ASC";
                }
                this.BindData2IdleInventoryRptList(idleInventoryRptQueryEntiry);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            IdleInventoryRptQueryEntiry idleInventoryRptQueryEntiry = new IdleInventoryRptQueryEntiry();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    idleInventoryRptQueryEntiry.OWNER_ID = this.PreviousPage.OwnerID;
                    idleInventoryRptQueryEntiry.VENDOR_ID = this.PreviousPage.VendorID;
                    idleInventoryRptQueryEntiry.MIN_DAYS = int.Parse(string.IsNullOrEmpty(this.PreviousPage.MinDays) ? "0" : this.PreviousPage.MinDays);
                    idleInventoryRptQueryEntiry.MAX_DAYS = int.Parse(string.IsNullOrEmpty(this.PreviousPage.MaxDays) ? "0" : this.PreviousPage.MaxDays);
                }
                else
                {
                    idleInventoryRptQueryEntiry.VENDOR_ID = "";
                    idleInventoryRptQueryEntiry.OWNER_ID = "";
                    idleInventoryRptQueryEntiry.MIN_DAYS = 0;
                    idleInventoryRptQueryEntiry.MAX_DAYS = 0;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            idleInventoryRptQueryEntiry.IsGetAll = false;
            idleInventoryRptQueryEntiry.CurrentPage = "0";
            idleInventoryRptQueryEntiry.PageSize = this.gvIdleInventoryRpt.PageSize.ToString();
            idleInventoryRptQueryEntiry.SortField = "SKU_ID";
            idleInventoryRptQueryEntiry.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvIdleInventoryRpt.PageSize;
            this.SetIdleInventoryRptQueryEntiry(idleInventoryRptQueryEntiry);
            this.BindData2IdleInventoryRptList(idleInventoryRptQueryEntiry);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetIdleInventoryRptQueryEntiry(IdleInventoryRptQueryEntiry idleInventoryRptQueryEntiry)
        {
            base.PageQueryEntitySession = idleInventoryRptQueryEntiry;
        }

        public IdleInventoryRptQuery PreviousPage
        {
            get
            {
                return (IdleInventoryRptQuery) base.PreviousPage;
            }
        }
    }
}

